import Vue from 'vue'

import VueRouter from 'vue-router'

import axios from 'axios'
import VueAxios from 'vue-axios'



import App from './App.vue'
import router from './router'

//导入需要验证token请求路径的列表名单
import {validTokenUrls} from './js/validToken'

//导入flexible设置rem基准值
import 'lib-flexible/flexible'

//导入vant组件
import { 
  Tabbar, 
  TabbarItem,
  Search,
  Sidebar, 
  SidebarItem,
  Skeleton,
  Toast,
  NavBar,
  Stepper,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Form,
  Field,
  Button,
  Popup,
  SwipeCell,
  Checkbox,
  SubmitBar,
  Empty,
  Image as VanImage,
  Icon,
  Swipe,
  SwipeItem,
  Lazyload,
  AddressList,
  AddressEdit,
  Tag,
  Uploader,
  NumberKeyboard,
  PasswordInput,
  Tab,
  Tabs,
  Cell,
  CellGroup,
  Dialog   
} from 'vant';
import { areaList } from '@vant/area-data';
//注册vant组件
Vue
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(Sidebar)
  .use(SidebarItem)
  .use(Skeleton)
  .use(Toast)
  .use(NavBar)
  .use(Stepper)
  .use(GoodsAction)
  .use(GoodsActionIcon)
  .use(GoodsActionButton)
  .use(Form)
  .use(Field)
  .use(Button)
  .use(Popup)
  .use(SwipeCell)
  .use(Checkbox)
  .use(SubmitBar)
  .use(Empty)
  .use(VanImage )
  .use(Icon )
  .use(Swipe)
  .use(SwipeItem)
  .use(Lazyload)
  .use(AddressList)
  .use(AddressEdit)
  .use(areaList )
  .use( Tag)
  .use(Uploader )
  .use( NumberKeyboard)
  .use(PasswordInput)
  .use(Tab)
  .use(Tabs)
  .use(Cell)
  .use(CellGroup)
  .use(Dialog)
Vue.config.productionTip = false

//设置默认基础请求路径
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002';


// 添加请求拦截器(请求之前触发)
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么

  //   
  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  let tokenString = sessionStorage.getItem('token');


  if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;

    if (validTokenUrls.indexOf(config.url) > -1) {
      config.params.tokenString = tokenString;
    }

  } else if (config.method === 'post') {

    //对post请求参数进行序列化 a=1&b=2&....
    

    config.data = config.data || {};

    if (validTokenUrls.indexOf(config.url) > -1) {
      config.data.tokenString = tokenString;
    }

    let paramsString = '';

    for (let key in config.data) {
      paramsString += `${key}=${config.data[key]}&`;
    }

    paramsString += `appkey=${appkey}`;

    config.data = paramsString;

    // 
  }

  // 

  //启动加载提示
  Toast.loading({
    message: '加载中...',
    duration: 0
  });

  return config;

}, error => {
  // 对请求错误做些什么
  //关闭加载提示
  Toast.clear();
  return Promise.reject(error);
});

// 添加响应拦截器(响应之前触发)
axios.interceptors.response.use(response => {
  // 对响应数据做点什么
  // 
  //关闭加载提示
  Toast.clear();

  return response;
}, error => {
  // 对响应错误做点什么
  //关闭加载提示
  Toast.clear();
  return Promise.reject(error);
});


Vue.use(VueAxios, axios)

//防止点击相同路由出现错误(此错误不会后续操作)
let naturalPush = VueRouter.prototype.push;

//重写路由原型的push方法, 在push方法添加catch方法捕获错误
VueRouter.prototype.push = function (location) {
  return naturalPush.call(this, location).catch(err => {});
}
//日期过滤器
Vue.filter('formatDate', value => {
  // 
  if (Object.prototype.toString.call(value) !== "[Object Date]") {
      value = new Date(value);
  }
  var year = value.getFullYear();
  var month = value.getMonth() + 1;
  month = month >= 10 ? month : '0' + month;
  var date = value.getDate();
  date = date >= 10 ? date : '0' + date;
  var hours = value.getHours();
  hours = hours >= 10 ? hours : '0' + hours;
  var minutes = value.getMinutes();
  minutes = minutes >= 10 ? minutes : '0' + minutes;
  var seconds = value.getSeconds();
  seconds = seconds >= 10 ? seconds : '0' + seconds;
  return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
})


new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
